package com.kuqi;

import android.os.Bundle;
import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ImageView;
import android.widget.ProgressBar;

import androidx.annotation.Nullable;

import com.kuqi.core.AppActivity;

public class WebActivity extends AppActivity {

    // 界面控件
    private ImageView iv_game_background;
    private WebView wv_game_control_win;
    private ProgressBar pb_game_loading;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.web_game_page);

        // 隐藏标题栏
        hideActionBar();

        // 获取控件
        iv_game_background = findViewById(R.id.iv_game_background);
        wv_game_control_win = findViewById(R.id.wv_game_control_win);
        pb_game_loading = findViewById(R.id.pb_game_loading);

        // 启动加载
        initWebViewShown();
    }


    /**
     * 测试初始化页面显示（测试）
     */
    private void initWebViewShown(){
        // 打开加载进度条
        // pb_game_loading.setVisibility(View.VISIBLE);
        // 由于我们的游戏都是js生成的，需要开启支持（小心应用）
        WebSettings webSettings = wv_game_control_win.getSettings();
        webSettings.setJavaScriptEnabled(true);
        // 不使用缓存
        wv_game_control_win.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);
        // 设置加载进度监听
        // wv_game_control_win.setWebChromeClient(chromeClient);
        // 设置使其可以在webView中打开显示，而不是跳转浏览器
        wv_game_control_win.setWebViewClient(viewClient);
        // 设置网址并加载
        wv_game_control_win.loadUrl("http://122.9.153.108:8080/app_server/app/testGameHtml.html");
        wv_game_control_win.requestFocus();
    }

    /**
     * 不让他跳转浏览器，而是本地打开
     * 返回值是true的时候控制网页在WebView中打开，
     * 为false的时候，调用系统浏览器或者第三方浏览器打开
     */
    WebViewClient viewClient = new WebViewClient(){
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            view.loadUrl(url);
            return true;
        }
    };

    /**
     * 我们的游戏通常不小，设计个加载进度条是很必要的
     */
    WebChromeClient chromeClient= new WebChromeClient(){
        public void onProgressChanged(WebView view,int newProgress){
            if(newProgress==100){
                // 隐藏加载进度条
                pb_game_loading.setVisibility(View.GONE);
            }else{
                //加载中
                pb_game_loading.setProgress(newProgress);
            }
        }
    };

}
